package com.tbabs.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tbabs.common.R;
import com.tbabs.entity.Employee;
import com.tbabs.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee,
                             HttpServletRequest request){
        // 对提交的密码进行md5加密处理
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        // 数据库查询，返回查到的对象
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername, employee.getUsername());
        Employee employeeServiceOne = employeeService.getOne(queryWrapper);

        // 判断是否为空
        if (employeeServiceOne == null) {
            return R.error("用户不存在！");
        }
        // 密码比对
        if (!employeeServiceOne.getPassword().equals(password)) {
            return R.error("密码错误！");
        }
        // 查看员工状态
        if (employeeServiceOne.getStatus() == 0) {
            return R.error("该员工已被禁用！");
        }

        request.getSession().setAttribute("employee", employeeServiceOne.getId());
        return R.success(employeeServiceOne);
    }

    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        // 清除session
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    @PostMapping
    public R<String> saveEmployee(@RequestBody Employee employee){
        // 初始化密码
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

        employeeService.save(employee);
        return R.success("新增成功！");
    }

    @GetMapping("/page")
    public R<Page<Employee>> getEmployeeByPage(int page, int pageSize,
                                               @RequestParam(required = false)String name){
        // 分页构造器
        Page<Employee> pageInfo = new Page<>(page, pageSize);

        // 构造条件构造器
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(name), Employee::getName, name)
                .orderByDesc(Employee::getUpdateTime);

        // 执行查询
        employeeService.page(pageInfo, queryWrapper);

        return R.success(pageInfo);
    }

    @PutMapping
    public R<String> updateEmployee(@RequestBody Employee employee){
        employeeService.updateById(employee);

        return R.success("修改信息成功！");
    }

    @GetMapping("/{id}")
    public R<Employee> getEmployeeInfo(@PathVariable Long id){
        // 根据id获取对应的信息
        Employee employee = employeeService.getById(id);

        if (employee != null) {
            return R.success(employee);
        }
        return R.error("未查询到对应员工信息");
    }
}
